layui.use(['element', 'jquery', 'layer', 'laytpl', 'laypage', 'form', 'table'], function() {
	var $ = layui.jquery,
		layer = layui.layer,
		laytpl = layui.laytpl,
		laypage = layui.laypage,
		form = layui.form,
		element = layui.element,
		table = layui.table;

	if(document.getElementById("adname").value=='admin003'|| document.getElementById("adname").value == "管理员2") {
		var orderTb = table.render({
			elem: '#order_tb',
			url: '/order/list',
			cols: [
				[{
					field: 'orderId',
					title: '订单编号',
					width: 120,
					fixed: 'left',
					unresize: true,
					sort: true
				}, {
					field: 'consigneeName',
					title: '收货人姓名',
					width: 120
				}, {
					field: 'address',
					title: '收货地址',
					width: 220
				}, {
					field: 'zip',
					title: '邮政编码',
					width: 100,
					sort: true
				}, {
					field: 'phoneNumber',
					title: '联系方式',
					width: 120
				}, {
					field: 'status',
					title: '订单状态',
					width: 90,
					templet: function (res) {
						if (res.status) {
							return '<span class="layui-badge layui-bg-blue">已发货</span>';
						}
						return '<span class="layui-badge">待发货</span>';
					}
				}, {
					field: 'createTime',
					title: '创建时间',
					width: 170
				},{
					field: 'aftersale',
					title: '售后状态',
					width: 110,
					templet: function (res) {
						var after;
						$.ajax({
							url:'/aftersale/findbyorderid?orderId='+res.orderId,
							async: false,
							success:function (rgs) {
								console.log(rgs.data.pstatus);
								after = rgs.data.pstatus;
							}
						});
						if (after>2){
							switch (after) {
								case 3: return '<span class="layui-badge">申请退货中</span>';
								case 4: return '<span class="layui-badge">已同意退货</span>';
								case 5: return '<span class="layui-badge">申请换货中</span>';
								case 6: return '<span class="layui-badge">已同意换货</span>';
								case 7: return '<span class="layui-badge">已拒绝退货</span>';
								case 8: return '<span class="layui-badge">已拒绝换货</span>';
							}
						}else
							return '<span class="layui-badge">未有售后</span>';
					}
				}, {
					fixed: 'right',
					title: '操作',
					toolbar: '#order_tb_bar',
					width: 230
				}]
			],
			page: true,
			height: 450
		});
	}else {

		var orderTb = table.render({
			elem: '#order_tb',
			url: '/order/searchbybName?bName='+document.getElementById("adname").value,
			cols: [
				[{
					field: 'orderId',
					title: '订单编号',
					width: 120,
					fixed: 'left',
					unresize: true,
					sort: true
				}, {
					field: 'consigneeName',
					title: '收货人姓名',
					width: 120
				}, {
					field: 'address',
					title: '收货地址',
					width: 220
				}, {
					field: 'zip',
					title: '邮政编码',
					width: 100,
					sort: true
				}, {
					field: 'phoneNumber',
					title: '联系方式',
					width: 120
				}, {
					field: 'status',
					title: '订单状态',
					width: 90,
					templet: function (res) {
						if (res.status) {
							return '<span class="layui-badge layui-bg-blue">已发货</span>';
						}
						return '<span class="layui-badge">待发货</span>';
					}
				}, {
					field: 'createTime',
					title: '创建时间',
					width: 170
				},{
					field: 'aftersale',
					title: '售后状态',
					width: 110,
					templet: function (res) {
						var after;
						$.ajax({
							url:'/aftersale/findbyorderid?orderId='+res.orderId,
							async: false,
							success:function (rgs) {
								console.log(rgs.data.pstatus);
								after = rgs.data.pstatus;
							}
						});
						if (after>2){
							switch (after) {
								case 3: return '<span class="layui-badge">申请退货中</span>';
								case 4: return '<span class="layui-badge">已同意退货</span>';
								case 5: return '<span class="layui-badge">申请换货中</span>';
								case 6: return '<span class="layui-badge">已同意换货</span>';
								case 7: return '<span class="layui-badge">已拒绝退货</span>';
								case 8: return '<span class="layui-badge">已拒绝换货</span>';
							}
						}else
							return '<span class="layui-badge">未有售后</span>';
					}
				}, {
					fixed: 'right',
					title: '操作',
					toolbar: '#order_tb_bar',
					width: 230
				}]
			],
			page: true,
			height: 450
		});

	}

	//监听行工具事件
	table.on('tool(order_tb)', function(obj) {
		var data = obj.data;
		if (obj.event === 'del') {
			layer.confirm('真的删除行么', function(index) {
				$.ajax({
					url:'/order/list/'+data.orderId,
					type:'delete',
					dataType:'json',
					success:function (res) {
						if(res.code!=0){
							return layer.msg(res.msg, {icon: 2});
						}
						return layer.msg("删除成功", {icon: 1},function () {
							obj.del();
							$("#order-items").html("");
						});
					},
					error:function () {
						return layer.msg("服务器错误,请稍后再试", {icon: 2});
					}
				});
				layer.close(index);
			});
		} else if (obj.event === 'detail') {
			//渲染订单明细
			laytpl($("#order-item-tpl").html()).render(data.orderItems, function(html) {
				$("#order-items").html(html);
			});
		}else if (obj.event === 'edit') {
			layer.open({
				type: 1,
				title: '编辑订单',
				content: $("#order-edit-tpl").html(),
				area: ['350px'],
				btn: ['更新'],
				yes: function(index1) {
					let new_data=form.val("order-edit-form");
					if(new_data.status==null){
						new_data.status=false;
					}
					console.log(new_data);
					$.ajax({
						url: '/order/list/'+data.orderId,
						type: 'PUT',
						data: JSON.stringify(new_data),
						contentType: 'application/json',
						dataType: 'json',
						success: function(res) {
							if (res.code != 0) {
								return layer.msg(res.msg, {
									icon: 2
								});
							}
							return layer.msg("更新成功", {
								icon: 1,
								time: 1300
							}, function() {
								obj.update(new_data);
								layer.close(index1);
							});
						}
					});
				},
				success: function() {
					//填充表单（编辑状态）
					form.val("order-edit-form", data);
					form.render();
				}
			});
		} else if (obj.event === 'after'){
			var after;
			//根据pstatus状态进行操作
			var sta;
			var text;
			var text1
			$.ajax({
				url: '/aftersale/findbyorderid?orderId='+data.orderId,
				async: false,
				success:function (res) {
					//退货申请和换货申请,无非就是3或者5，同意退货请按4，拒绝请按7，同意换货按6，拒绝换货按8
					//console.log(res.data.pstatus);
					after = res.data.pstatus;
					text1 = res.data.jiajuName;
				}
			});

			if(after==3||after==5) {
				layer.open({
					type: 1,
					title: '售后服务',
					content: $("#order-after-tpl").html(),
					area: ['400px'],
					btn: ["同意", "拒绝"],
					yes: function (index1) {
						//console.log("start1");
						//同意按钮里，退货则赋值sta=4,换货则赋值sta=6
						if (after == 3) {
							sta = 4;
							text = "同意退货成功！";
						} else if (after == 5) {
							sta = 6;
							text = "同意换货成功！";
						}
						fff(data.orderId, sta, text, index1);
						console.log("123");
						layer.close(index1);
					},
					btn2: function (index1) {
						console.log("start2");
						//拒绝按钮里，退货则赋值sta=7,换货则赋值sta=8
						if (after == 3) {
							sta = 7;
							text = "拒绝退货成功！";
						} else if (after == 5) {
							sta = 8;
							text = "拒绝换货成功！";
						}
						fff(data.orderId, sta, text, index1);
						console.log("124");
					}
				});
				console.log(after);
				switch (after) {
					case 3:$("h3").text("收到一个退货请求，商品为："+text1);break;
					case 5:$("h3").text("收到一个换货请求，商品为："+text1);break;
				}
			}else
				return layer.msg("暂时无售后请求", {icon: 2});
		}
	});
function fff(orderId,sta,text,index1) {
	$.ajax({
		url: '/aftersale/updateafter?orderId='+orderId+'&pstatus='+sta,
		success:function (res) {
			return layer.msg(text, {icon: 1});
		},
		error:function () {
			return layer.msg("服务器错误,请稍后再试", {icon: 2});
		}, function() {
			layer.close(index1);
		}
	})

}
	//搜索
	var order_tb_this;
	form.on('submit(search_btn)', function(data) {
		if (order_tb_this != null) {
			order_tb_this.where = {};
		}
		orderTb.reload({
			url: '/order/search',
			where: data.field,
			page: {
				curr: 1
			},
			done: function() {
				order_tb_this = this;
			}
		});
		return false;
	});


});
